package net.csdn.www;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.Set;

public class Test18 {
	private static String URL = null;
	private static String[] IMG_REGEXS = null;
	private static String HTTP_REGEX = null;
	private static boolean WEB_PAGE_FLAG = false;
	private static int WEB_PAGE_MAX = 1;
	private static int WEB_PAGE_MIN = 1;
	private static String FILE_SAVE_PATH = null;

	private static Properties getPropertiesFile(URI uri) {
		Properties defaultProperties = null;
		try {
			File file = new File(uri);
			FileInputStream defaultInputStream = new FileInputStream(file);
			defaultProperties = new Properties();
			defaultProperties.load(defaultInputStream);
		} catch (IOException e) {
			System.out.println("ERROR:==load properties file fail==");
			System.out.println("THE SPECIFIC ERROR INFO : " + e);
			e.printStackTrace();
		}
		return defaultProperties;
	}

	private static void getPropertys(URI uri) {
		Properties defaultProperties = getPropertiesFile(uri);
		URL = defaultProperties.getProperty("url");
		HTTP_REGEX = defaultProperties.getProperty("http.regex");
		IMG_REGEXS = getStringArrayFromStr(
				defaultProperties.getProperty("image.regex"), ";");
		WEB_PAGE_FLAG = new Boolean(
				defaultProperties.getProperty("web.page.flag"));
		WEB_PAGE_MAX = Integer.parseInt(defaultProperties
				.getProperty("web.page.max"));
		WEB_PAGE_MIN = Integer.parseInt(defaultProperties
				.getProperty("web.page.min"));
		
		FILE_SAVE_PATH = defaultProperties.getProperty("file.save.path");
	}

	private static String[] getStringArrayFromStr(String str, String regex) {
		String[] strs = str.split(regex);
		return strs;
	}

	private static URI getRealClassPath() {
		URI uri = null;
		try {
			uri = Test01.class.getResource("/el.properties").toURI();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return uri;
	}

	/**
	 * 刘柱栋 2013-3-11
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// 初始化操作 ：： 读取配置文件的配置信息
		getPropertys(getRealClassPath());
		
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssms");
		String str = dateFormat.format(new Date());
		
		String path = FILE_SAVE_PATH + File.separator + str + File.separator;
		FileUtli.setFilePath(path + "logger.txt");
		
		SetImagesFromUrlInfo sifu = new SetImagesFromUrlInfo();
		sifu.setFilePath(path);
		sifu.setImageRegexs(IMG_REGEXS);
		sifu.setRegex(HTTP_REGEX);

		for (int i = 1; i < 1954; i++) {
			GetImagesFromUrl gifu = new GetImagesFromUrl(sifu);
			gifu.setUrl(URL + i);
			new Thread(gifu).start();
		}
	}

}

class GetImagesFromUrl implements Runnable {
	private SetImagesFromUrlInfo sifu;
	private String url;
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public GetImagesFromUrl(){}
	
	public GetImagesFromUrl(SetImagesFromUrlInfo sifu){
		this.sifu = sifu;
	}
	
	@Override
	public void run() {
		long startTimes = System.currentTimeMillis();
		Random random = new Random();
		int randomNumber = Math.abs( random.nextInt(1954));
		long i = randomNumber * 1000;
		try {
			Thread.sleep(i);
		} catch (InterruptedException e) {
			System.out.println(Thread.currentThread().getName() + e.toString());
			e.printStackTrace();
		}
		
		sifu.test(Thread.currentThread().getName(),url);
		long endTimes = System.currentTimeMillis();
		int min =(int)( (endTimes - startTimes) /1000 ) - randomNumber;
		System.out.println(Thread.currentThread().getName() + " cost times : " + min + "s");
	}

}

class SetImagesFromUrlInfo {
	private String regex;
	private String filePath;
	private String[] imageRegexs;

	public String[] getImageRegexs() {
		return imageRegexs;
	}

	public void setImageRegexs(String[] imageRegexs) {
		this.imageRegexs = imageRegexs;
	}

	public String getFilePath() {
		return filePath;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}


	public String getRegex() {
		return regex;
	}

	public void setRegex(String regex) {
		this.regex = regex;
	}

	public void test(String name,String url) {
		List<String> imagePageUrlList = UrlUtil.getHttpUrl(url, regex);
		
		if (imagePageUrlList != null) {
			Set<String> set = new HashSet<>(imagePageUrlList);
			for (String str : set) {
				FileUtli.outPutStringToFile(str);
				printList(UrlUtil.getImgs(str, imageRegexs),name);
			}
		}
	}

	private synchronized void printList(List<String> list,String name) {
		if (list != null) {
			Set<String> set = new HashSet<>(list);
			StringBuilder strb = new StringBuilder(200);
			strb.append("\r\n============================================================== \r\n");
			System.out.print(name + ":");
			for (String str : set) {
				ImageSave.saveImg(str, filePath);
				strb.append(str + "\r\n");
				System.out.print("=");
			}
			System.out.println();
			strb.append("==============================================================");
			FileUtli.outPutStringToFile(strb.toString());
		}
	}
	
	private synchronized void saveImage(String str){
		ImageSave.saveImg(str, filePath);
	}
}
